﻿function Application() {

    Application.superclass.constructor.call(this);


    var instance = this;

    var room;


    var history_text = "";
    var message = "";

    var nikname = (Math.floor(Math.random()*1000000)).toString();
    var transport;

    /*Constructor*/
    (function () {
        assign_default();
    })();

    /*Public section*/



    /*Private section*/

    function query_password() {

        document.getElementById("main").innerHTML =
            "<div id='pass_title'>Input Password</div>"+
        "<input type='password' id='password_field'/>" +
        "<input type='button' value='Submit' id='btn_submit'/>"+
        "<input type='button' value='Cancel' id='btn_cancel'/>";

        transport = new Transport(callback, 1);

        assign_query_password();
    }
    

    function sign_in(room_num) {
        room = room_num;
        document.getElementById("main").innerHTML = 
            "<div id='history'>  </div>" +
            "<input type='button' id='btn_close' value='X'/>"+
            "<input type='text' id='text_field' />" +
            "<input type='button' id='btn_send' value='Send'/>" ;

        assign_sign_in();

        transport = new Transport(callback, room);

        history_text = transport.GetChatForRoom(room);
        history_update();

    }

    function default_action() {

        document.getElementById("main").innerHTML =
        "<div id='label'>Choose the room</div>"+
        "<input type='button' value='Public' id='btn_public'/>"+
        "<input type='button' value='Private' id='btn_private'/>";

        assign_default();
    }


    /*Assign listeners functions*/

    function assign_default() {

        document.getElementById("btn_public").onclick = function () {
            sign_in(0);
        };

        document.getElementById("btn_private").onclick = function () {
            query_password();
        };
    };

    function assign_query_password() {

        document.getElementById("btn_cancel").onclick = function () {
            default_action();
        };

        document.getElementById("btn_submit").onclick = function () {
            
            var passw = document.getElementById("password_field").value;
            var status = transport.Validate(passw);

            if (status) {
                sign_in(1);
                assign_sign_in();
            }
            else {
                document.getElementById("password_field").value = "";
                document.getElementById("pass_title").innerHTML = "Invalid password";
            }
        };
    }

    function assign_sign_in() {
        document.getElementById("btn_send").onclick = function () {
            message = document.getElementById("text_field").value.toString();
            transport.SendMessage(
                message, nikname);
            document.getElementById("text_field").value = "";
        };

        document.getElementById("btn_close").onclick = function () {
            default_action();
        };
    }

    function history_update() {
        if(document.getElementById("history"))
        {
            document.getElementById("history").innerHTML = history_text;
        };
    }


    function callback(text) {
        history_text = text;
        history_update();
    }

}


window.extendClass(Application, Object);



